package com.stock.db;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.stock.model.StockInfo;

public class StockInfoDAO extends BaseDAO{
	  public List<StockInfo> queryAll() throws SQLException {
		  String sql ="SELECT * from STOCK_INFO";
		  return this.getQueryRunner().query(sql, new  BeanListHandler<>(StockInfo.class));
	  }
	  
	  public List querySymbol() throws SQLException {
		  String sql ="SELECT symbol from STOCK_INFO";
		  return this.getQueryRunner().query(sql, new ArrayListHandler ());
	  }
	  
	  
	  public void insert(StockInfo stockInfo) throws SQLException {
		  String insertSql = "INSERT INTO STOCK_INFO (symbol,name,note) VALUES(?,?,?)";
		  String delSql = "DELETE FROM STOCK_INFO WHERE symbol=?";
		  
		  QueryRunner queyr = this.getQueryRunner(); 
		  queyr.update(delSql, stockInfo.getSymbol());
		  PreparedStatement ps = getConnection().prepareStatement(insertSql);
		  queyr.fillStatementWithBean(ps, stockInfo, "symbol", "name", "note");
		  ps.execute();
		  ps.close();
	  }
}
